package com.pro.ydl;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.TabActivity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.provider.ContactsContract.Data;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.BasicStroke;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;


enum EM_CHART_TYPE
{
	EM_CHART_TYPE_LINE,
	EM_CHART_TYPE_BAR
};
public class YDLChartFactory {
	
	XYMultipleSeriesDataset dataset;
	XYMultipleSeriesRenderer renderer;
	GraphicalView GVTable;
	EM_CHART_TYPE emChartType;
	private String sLineTable="YDLLineTable";
	private String sBarTable="YDLBarTable";
	public YDLChartFactory(EM_CHART_TYPE type, String sPromptMessage)
	{
		sLineTable = sPromptMessage;
		sBarTable = sPromptMessage;
		dataset = new XYMultipleSeriesDataset();
		renderer = new XYMultipleSeriesRenderer();
		if(EM_CHART_TYPE.EM_CHART_TYPE_LINE == type)
		{
			emChartType = EM_CHART_TYPE.EM_CHART_TYPE_LINE;
		}
		else
		{
			emChartType = EM_CHART_TYPE.EM_CHART_TYPE_BAR;		
		}
	}

	/***************************LineChart Section***********************************/
	public  void setYDLLineChartCoordinate(double[] x, double[]y)
	{
		if((null == dataset) || (null == renderer ))
		{
			return;
		}
		if(x.length != y.length)
		{
			return;
		}	
		XYSeries series = new XYSeries(sLineTable, 0);
		for(int iPointNum = 0;iPointNum < x.length;iPointNum++)
		{
			series.add(x[iPointNum], y[iPointNum]);
		}
		double[] temp = new double[x.length];
		String strXLabel= "";
		int hour = 2;
		for(int i = 0;i<temp.length;i++)
		{
			
			if(temp.length == 2880)
			{
				if(i > 0 && i%240 == 0)
				{
					temp[i] = hour;
					hour = hour + 2;
					strXLabel = String.valueOf((int)temp[i]);
				}
				else
				{
					strXLabel = "";
				}
			}
			renderer.addXTextLabel(i, strXLabel);
		}
		dataset.addSeries(series);
		
		if((null == dataset) || (null == renderer ))
		{
			return;
		}
		//����x��y��ı������ִ�С
		renderer.setAxisTitleTextSize(25);
		//�������ͼ��ı������ִ�С
		renderer.setChartTitleTextSize(30);
		//����x��y��ĵ�λ�̶�������ʾ��С
		renderer.setLabelsTextSize(15);
		//����ͼ�����ִ�С
		renderer.setLegendTextSize(20);
		//set AxisTitle color
		renderer.setLabelsColor(Color.parseColor("#2F8300"));
		//�����޵���Ч��
		renderer.setInScroll(true);
		//设置可touch
		renderer.setClickEnabled(true);
		//���ÿ̶ȵ�������ɫ		
		renderer.setXLabelsColor(Color.parseColor("#2F8300"));
		//first parameter ����ĸ�renderer scale
		renderer.setYLabelsColor(0,Color.parseColor("#2F8300"));
		//����ͼ���Ƿ�������������϶�
		renderer.setPanEnabled(false, false);
		//������ʾ�Ŵ�ť
		renderer.setZoomButtonsVisible(false);
		//���ñ���
		renderer.setBackgroundColor(Color.parseColor("#EFF8F7"));
		renderer.setApplyBackgroundColor(true);		
		//����ͼ���ϵ�Ĵ�С��ͬ��ͼ���ϱ�ʾҲ��ı�
		//renderer.setPointSize(2.5f);
		//�Ƿ���ʾ���
		renderer.setShowGrid(true);

		//���ÿ̶ȵ�λ��
	    renderer.setXLabelsAlign(Align.CENTER);  
	    renderer.setYLabelsAlign(Align.LEFT);
	    //����margin����ɫ
	    renderer.setMarginsColor(Color.parseColor("#EFF8F7"));
	    
	    //y�����С��ݣ��϶��Ǵ�0��ʼ
	    renderer.setYAxisMin(0); 
		//����y����ʾ��䷶Χ
		renderer.setYAxisMax(series.getMaxY()+series.getMaxY()*0.1);
		//x������Ϊ1
		renderer.setXAxisMin(1);
		//����x������ֵ
		renderer.setXAxisMax(series.getMaxX());
		////set labels for x axis,if set renderer.setXLabels(0),no lab will show
		renderer.setXLabels(0);
		
		XYSeriesRenderer r = new XYSeriesRenderer();
		r.setColor(Color.parseColor("#205E00"));
		r.setPointStyle(PointStyle.POINT);
		r.setFillPoints(true);
		renderer.addSeriesRenderer(r);
	}
	
	public void setYDLLineChartSettings(String title, String xTitle, String yTitle) {
		//����ͼ�����
		renderer.setChartTitle(title);
		//����x�����
		renderer.setXTitle(xTitle);
		//����y�����
		renderer.setYTitle(yTitle);
		
		renderer.setShowAxes(true);

	}
	
	/***************************LineChart Section End***********************************/	
	/***************************BarChart Section ***********************************/
	public void setYDLBarChartCoordinate(double[] x,double[] y ) 
	{
		if((null == dataset) || (null == renderer ))
		{
			return;
		}
		if(x.length != y.length)
		{
			return;
		}
		XYSeries series = new XYSeries(sBarTable);
		int iDataSum = x.length;
		String temp1 = "";
		int temp2 = 0;
		double[] temp = new double[x.length];
		for(int i = 0;i<temp.length;i++)
		{
			if(temp.length < 8)
			{
				if(i ==0)
				{
					temp[i] = 7;
				}
				else
				{
					temp[i] = i;
				}
			}
			else if(temp.length < 15)
			{
				temp[i] = i+1;
			}
			else if(temp.length < 35)
			{
				temp[i] = i+1;
			}
			else
			{
				temp[i] = i+1;
			}
		}
		for (int k = 0; k < iDataSum; k++)
		{
			series.add(x[k],y[k]);
			temp2 = (int)temp[k];
			temp1 =   String.valueOf(temp2);
			renderer.addXTextLabel(k +1, temp1);
		}
		
		dataset.addSeries(series);
		/**************set bar chart renderer**************/

		//����x��y��ı������ִ�С
		renderer.setAxisTitleTextSize(25);
		//�������ͼ��ı������ִ�С
		renderer.setChartTitleTextSize(30);
		//ͼ���Ͽ̶ȵ����ִ�С
		renderer.setLabelsTextSize(15);
		//ͼ������ִ�С
		renderer.setLegendTextSize(20);
		//set AxisTitle color
		renderer.setLabelsColor(Color.parseColor("#2F8300"));
		//�����޵���Ч��
		renderer.setInScroll(true);
		renderer.setClickEnabled(true);
		//���ÿ̶ȵ�������ɫ		
		renderer.setXLabelsColor(Color.parseColor("#2F8300"));
		//first parameter ����ĸ�renderer scale
		renderer.setYLabelsColor(0,Color.parseColor("#2F8300"));
		
		//����ͼ���Ƿ�������������϶�
		renderer.setPanEnabled(false, false);
		//������ʾ�Ŵ�ť
		renderer.setZoomButtonsVisible(false);
		//���Ӽ�ļ��
		renderer.setBarSpacing(0.5f);
		//���ñ���
		renderer.setBackgroundColor(Color.parseColor("#EFF8F7"));
		renderer.setApplyBackgroundColor(true);
		//�Ƿ���ʾ���
		renderer.setShowGrid(false);
		//���ÿ̶ȵ�λ��
	    renderer.setXLabelsAlign(Align.CENTER);  
	    renderer.setYLabelsAlign(Align.LEFT);
	    //����margin����ɫ
	    renderer.setMarginsColor(Color.parseColor("#EFF8F7"));
	    
	    //y�����С��ݣ��϶��Ǵ�0��ʼ
	    renderer.setYAxisMin(0); 
		//����y����ʾ��䷶Χ
		renderer.setYAxisMax(series.getMaxY()+series.getMaxY()*0.1);
		//x������Ϊ0��ԭ�������̬����Ϊx��ݵĻ�����ʾ���ÿ�
		renderer.setXAxisMin(0);
		//+1��ԭ����Ϊ����ʾ���һ������
		renderer.setXAxisMax(series.getMaxX()+1);
		//set labels for x axis,if set renderer.setXLabels(0),no lab will show
		//renderer.setXLabels(series.getItemCount()+1);
		renderer.setXLabels(0);
		SimpleSeriesRenderer r = new SimpleSeriesRenderer();
		r.setChartValuesTextSize(20);
		r.setColor(Color.parseColor("#2F8300"));
		r.setDisplayChartValues(false);
		renderer.addSeriesRenderer(r);
	}
	public void setYDLBarChartSettings(String title, String xTitle, String yTitle)
	{
		//����ͼ�����
		renderer.setChartTitle(title);
		//����x�����
		renderer.setXTitle(xTitle);
		//����y�����
		renderer.setYTitle(yTitle);

	}
	/***************************BarChart Section End***********************************/
	public GraphicalView getGVview(Activity context)
	{
		if((null == dataset) || (null == renderer ))
		{
			return null;
		}
		if(EM_CHART_TYPE.EM_CHART_TYPE_LINE == emChartType)
		{
			GVTable = ChartFactory.getLineChartView(context, dataset, renderer);
		}
		else
		{
			GVTable = ChartFactory.getBarChartView(context, dataset, renderer, Type.DEFAULT);
		}
		return (GVTable);
	}


}
